import Koa from 'koa';
import { readFile } from 'fs/promises';

const app = new Koa();

const pageHandler = async(htmlName) => {
    try {
        const filePath = `./view/${htmlName}`;
        const file = await readFile(filePath, 'binary');
        return file;
    } catch (error) {
        console.log(error);
    }

}

const routerHandler = async(url) => {
    switch (url) {
        case '/index':
        case '/':
            return 'index.html';
        case '/todo':
            return 'todo.html';
        default:
            return '404.html';
    }
}

app.use(async(ctx) => {
    const url = ctx.request.url;
    const htmlName = await routerHandler(url);
    const htmlPage = await pageHandler(htmlName);
    ctx.body = htmlPage;
})



app.listen(3000);